<?php
// 本类由系统自动生成，仅供测试用途
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends BaseController {
    public function index(){
		$this->display();
    }
    // 文章上传
    public function articleUploads(){
        $this->init();
    	$this->display();
    }
    // 文章提交
    public function articleSave(){
        // var_dump($_POST);
        $article=M('article');

        $data['title'] = I('post.title');
        $data['content'] = I('post.article_edit');
        $data['type'] = I('post.type');
        $data['recommended']=I('post.recommended');
        $data['time'] = time();
        $config = array(
		    'maxSize'    =>    3145728,
		    'savePath'   =>    '/article_img/',
		    'saveName'   =>    array('uniqid',''),
		    'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
		    'autoSub'    =>    false,
		    'subName'    =>    array('date','Ymd'),
		    'replace'    =>    true,
		);
		$upload = new \Think\Upload($config);
		if(!empty($_FILES['article_img']['tmp_name'])){
			$info = $upload->uploadOne($_FILES['article_img']);
		    $data['main_img'] = "Uploads".$info['savepath'].$info['savename'];
        }else{
            $info=true;
        }
//      var_dump($_FILES);
        $articleSave=$article->add($data);
        if($articleSave){
            $this->success("提交成功",U("Article/articleList"));
        }else{
            $this->error('提交失败');
        }
        // $a =$article->where('id=2')->find();
        // $a['content']=html_entity_decode($a['content']);
        // $this->assign('data',$a);
        // $this->success('提交成功','articleList');
    }

    // 文章列表
    public function articleList(){
        $this->init();
        $article=M('article');
        $articleList =$article->order('time desc')->select();
        for($i=0;$i<count($articleList);$i++){
            $titleList[$i]=$articleList[$i]['title']; 
        }
        $this->assign('articleList',$articleList);
        // var_dump($titleList);
    	$this->display();
    }
    // 删除文章
    public function deleteArticle(){
        $article=M('article');
        $deleteArticle=$article->where('id='.$_GET['id'])->delete();
        if($deleteArticle){
            $this->success("删除成功",U("Article/articleList"));
        }else{
            $this->error('删除失败');
        }
        // $this->redirect('articleList', 5, '页面跳转中...');
    }
    // 修改文章
    public function reviseArticle(){
        $this->init();
        
        $article=M('article');
        $reviseArticle=$article->where('id='.$_GET['id'])->select();
        $reviseArticle[0]['content']=html_entity_decode($reviseArticle[0]['content']);
        $this->assign('reviseArticle',$reviseArticle);
        $this->display();
    }
    //文章修改保存
    public function reviseArticleSave(){
        $article=M('article');

        $data['title'] = I('post.title');
        $data['content'] = I('post.article_edit');
        $data['type'] = I('post.type');
        $data['recommended']=I('post.recommended');
        $data['time'] = time();
        $config = array(
            'maxSize'    =>    3145728,
            'savePath'   =>    '/article_img/',
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    false,
            'subName'    =>    array('date','Ymd'),
            'replace'    =>    true,
        );
        $upload = new \Think\Upload($config);
        if(!empty($_FILES['article_img']['tmp_name'])){
            $info = $upload->uploadOne($_FILES['article_img']);
            $data['main_img'] = "Uploads".$info['savepath'].$info['savename'];
        }else{
            $info=true;
        }
//      var_dump($_FILES);
        $reviseArticleSave=$article->where('id='.$_GET['id'])->save($data);
        if($reviseArticleSave){
            $this->success("修改成功",U("Article/articleList"));
        }else{
            $this->error('修改失败');
        }
    }
}